using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace MONA.Modules.Marketing.BLL
{
    public partial class Customer
    {
        #region Private members.

        protected int _ID;
        protected string _Name = string.Empty;
        protected string _Location = string.Empty;
        protected string _Description = string.Empty;
        protected int _ContactID;
        protected int _ManagerID;
        protected int _CustomerTypeID;
        protected bool _IsOwner;

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public string Name
        {
            set { this._Name = value; }
            get { return this._Name; }
        }

        public string Location
        {
            set { this._Location = value; }
            get { return this._Location; }
        }

        public string Description
        {
            set { this._Description = value; }
            get { return this._Description; }
        }

        public int ContactID
        {
            set { this._ContactID = value; }
            get { return this._ContactID; }
        }

        public int ManagerID
        {
            set { this._ManagerID = value; }
            get { return this._ManagerID; }
        }

        public int CustomerTypeID
        {
            set { this._CustomerTypeID = value; }
            get { return this._CustomerTypeID; }
        }

        public bool IsOwner
        {
            set { this._IsOwner = value; }
            get { return this._IsOwner; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static Customer Load(int iD)
        {
            string spName = "[dbo].p_MARKETING_Customer_Load";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, iD);
            Customer entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Customer();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) entity.Name = reader.GetString(reader.GetOrdinal("Name"));
                if (!reader.IsDBNull(reader.GetOrdinal("Location"))) entity.Location = reader.GetString(reader.GetOrdinal("Location"));
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) entity.Description = reader.GetString(reader.GetOrdinal("Description"));
                if (!reader.IsDBNull(reader.GetOrdinal("ContactID"))) entity.ContactID = reader.GetInt32(reader.GetOrdinal("ContactID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ManagerID"))) entity.ManagerID = reader.GetInt32(reader.GetOrdinal("ManagerID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CustomerTypeID"))) entity.CustomerTypeID = reader.GetInt32(reader.GetOrdinal("CustomerTypeID"));
                if (!reader.IsDBNull(reader.GetOrdinal("IsOwner"))) entity.IsOwner = reader.GetBoolean(reader.GetOrdinal("IsOwner"));
            }
            reader.Close();
            dbCommand.Connection.Close();

            return entity;
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectBy_ContactID(int contactID)
        {
            string spName = "[dbo].p_MARKETING_Customer_SelectBy_ContactID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ContactID", SqlDbType.Int, contactID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------
        public static DataSet SelectBy_ManagerID(int managerID)
        {
            string spName = "[dbo].p_MARKETING_Customer_SelectBy_ManagerID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ManagerID", SqlDbType.Int, managerID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------
        public static DataSet SelectBy_CustomerTypeID(int customerTypeID)
        {
            string spName = "[dbo].p_MARKETING_Customer_SelectBy_CustomerTypeID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CustomerTypeID", SqlDbType.Int, customerTypeID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------

        public static DataSet SelectAll()
        {
            string spName = "[dbo].p_MARKETING_Customer_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll()
        {
            string spName = "[dbo].p_MARKETING_Customer_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_MARKETING_Customer_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_MARKETING_Customer_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int Insert(string name, string location, string description, int contactID, int managerID, int customerTypeID, bool isOwner)
        {
            Customer entity = new Customer();
            entity.Name = name;
            entity.Location = location;
            entity.Description = description;
            entity.ContactID = contactID;
            entity.ManagerID = managerID;
            entity.CustomerTypeID = customerTypeID;
            entity.IsOwner = isOwner;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            string spName = "[dbo].p_MARKETING_Customer_Insert";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@Name", SqlDbType.NVarChar, this._Name);
            db.AddInParameter(dbCommand, "@Location", SqlDbType.NVarChar, this._Location);
            db.AddInParameter(dbCommand, "@Description", SqlDbType.NVarChar, this._Description);
            db.AddInParameter(dbCommand, "@ContactID", SqlDbType.Int, this._ContactID);
            db.AddInParameter(dbCommand, "@ManagerID", SqlDbType.Int, this._ManagerID);
            db.AddInParameter(dbCommand, "@CustomerTypeID", SqlDbType.Int, this._CustomerTypeID);
            db.AddInParameter(dbCommand, "@IsOwner", SqlDbType.Bit, this._IsOwner);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
            else
            {
                db.ExecuteNonQuery(dbCommand);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int Update(int iD, string name, string location, string description, int contactID, int managerID, int customerTypeID, bool isOwner)
        {
            Customer entity = new Customer();
            entity.ID = iD;
            entity.Name = name;
            entity.Location = location;
            entity.Description = description;
            entity.ContactID = contactID;
            entity.ManagerID = managerID;
            entity.CustomerTypeID = customerTypeID;
            entity.IsOwner = isOwner;
            return entity.Update();
        }

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            string spName = "[dbo].p_MARKETING_Customer_Update";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);
            db.AddInParameter(dbCommand, "@Name", SqlDbType.NVarChar, this._Name);
            db.AddInParameter(dbCommand, "@Location", SqlDbType.NVarChar, this._Location);
            db.AddInParameter(dbCommand, "@Description", SqlDbType.NVarChar, this._Description);
            db.AddInParameter(dbCommand, "@ContactID", SqlDbType.Int, this._ContactID);
            db.AddInParameter(dbCommand, "@ManagerID", SqlDbType.Int, this._ManagerID);
            db.AddInParameter(dbCommand, "@CustomerTypeID", SqlDbType.Int, this._CustomerTypeID);
            db.AddInParameter(dbCommand, "@IsOwner", SqlDbType.Bit, this._IsOwner);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int Delete(int iD)
        {
            Customer entity = new Customer();
            entity.ID = iD;
            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            string spName = "[dbo].p_MARKETING_Customer_Delete";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        #endregion
    }
}